package com.gungnir.trial.impl;

import com.gungnir.trial.ThreeRateService;
import com.gungnir.trial.entity.ThreeRate;
import com.tyrfing.exceptions.TyrfingServiceException;
import com.gungnir.trial.mapper.threerate.ThreeRateMapper;
import com.tyrfing.model.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Implementation of ThreeRateService using ThreeRateMapper for MongoDB interactions.
 */
@Service
public class ThreeRateServiceImpl implements ThreeRateService {

    private final ThreeRateMapper threeRateMapper;
    private final Logger log = LoggerFactory.getLogger(this.getClass());

    // Constructor injection for ThreeRateMapper
    public ThreeRateServiceImpl(ThreeRateMapper threeRateMapper) {
        this.threeRateMapper = threeRateMapper;
    }

    @Override
    public void insertThreeRate(ThreeRate threeRate) throws TyrfingServiceException {
        try {
            threeRateMapper.insertThreeRate(threeRate);
        } catch (Exception e) {
            log.error("Failed to insert ThreeRate: {}", threeRate, e);
            throw TyrfingServiceException.someThingWentWrong();
        }
    }

    @Override
    public void countAndInsertThreeRate(int shouldAttendance, int attendanceFaceCount, int attendanceCount, int frontAttendanceCount, int clroFrontSeatCount, String classRoomCode, String teachingClassCode, Long imagShotTime) throws TyrfingServiceException {
        try {
            threeRateMapper.countAndInsertThreeRate(shouldAttendance, attendanceFaceCount, attendanceCount, frontAttendanceCount, clroFrontSeatCount, classRoomCode, teachingClassCode, imagShotTime);
        } catch (Exception e) {
            log.error("Failed to count and insert ThreeRate", e);
            throw TyrfingServiceException.someThingWentWrong();
        }
    }

    @Override
    public void deleteThreeRateByPrimaryKey(Long imagShotTime, String classRoomCode) throws TyrfingServiceException {
        try {
            threeRateMapper.deleteByPrimaryKey(imagShotTime, classRoomCode);
        } catch (Exception e) {
            log.error("Failed to delete ThreeRate with imagShotTime: {} and classRoomCode: {}", imagShotTime, classRoomCode, e);
            throw TyrfingServiceException.someThingWentWrong();
        }
    }

    @Override
    public void updateThreeRate(ThreeRate threeRate) throws TyrfingServiceException {
        try {
            threeRateMapper.updateThreeRate(threeRate);
        } catch (Exception e) {
            log.error("Failed to update ThreeRate: {}", threeRate, e);
            throw TyrfingServiceException.someThingWentWrong();
        }
    }

    @Override
    public Pagination<ThreeRate> findThreeRateByTimeRangePaged(Integer pageSize, Integer pageNum, Long startTime, Long endTime) throws TyrfingServiceException {
        try {
            return threeRateMapper.findByTimeRangePaged(pageSize,pageNum,startTime, endTime);
        } catch (Exception e) {
            log.error("Failed to find ThreeRate by time range: {} - {}", startTime, endTime, e);
            throw TyrfingServiceException.someThingWentWrong();
        }
    }

    @Override
    public Pagination<ThreeRate> findThreeRateByClassRoomCodePaged(Integer pageSize, Integer pageNum,String classRoomCode) throws TyrfingServiceException {
        try {
            return threeRateMapper.findByClassRoomCodePaged(pageSize,pageNum,classRoomCode);
        } catch (Exception e) {
            log.error("Failed to find ThreeRate by classRoomCode: {}", classRoomCode, e);
            throw TyrfingServiceException.someThingWentWrong();
        }
    }

    @Override
    public Pagination<ThreeRate> findThreeRateByTeachingClassCodePaged(Integer pageSize, Integer pageNum,String teachingClassCode) throws TyrfingServiceException {
        try {
            return threeRateMapper.findByTeachingClassCodePaged(pageSize,pageNum,teachingClassCode);
        } catch (Exception e) {
            log.error("Failed to find ThreeRate by teachingClassCode: {}", teachingClassCode, e);
            throw TyrfingServiceException.someThingWentWrong();
        }
    }

    @Override
    public Pagination<ThreeRate> findThreeRateByTimeRangeAndClassRoomCodePaged(Integer pageSize, Integer pageNum,Long startTime, Long endTime, String classRoomCode) throws TyrfingServiceException {
        try {
            return threeRateMapper.findByTimeRangeAndClassRoomCodePaged(pageSize,pageNum,startTime, endTime, classRoomCode);
        } catch (Exception e) {
            log.error("Failed to find ThreeRate by time range: {} - {} and classRoomCode: {}", startTime, endTime, classRoomCode, e);
            throw TyrfingServiceException.someThingWentWrong();
        }
    }

    @Override
    public Pagination<ThreeRate> findThreeRateByTimeRangeAndTeachingClassCodePaged(Integer pageSize, Integer pageNum,Long startTime, Long endTime, String teachingClassCode) throws TyrfingServiceException {
        try {
            return threeRateMapper.findByTimeRangeAndTeachingClassCodePaged(pageSize,pageNum,startTime, endTime, teachingClassCode);
        } catch (Exception e) {
            log.error("Failed to find ThreeRate by time range: {} - {} and teachingClassCode: {}", startTime, endTime, teachingClassCode, e);
            throw TyrfingServiceException.someThingWentWrong();
        }
    }

    @Override
    public ThreeRate findLatestByTeachingClassCodeOrClassRoomCode(String teachingClassCode, String classRoomCode) throws TyrfingServiceException {
        try {
            return threeRateMapper.findLatestByTeachingClassCodeOrClassRoomCode(teachingClassCode, classRoomCode);
        } catch (Exception e) {
            log.error("Failed to find latest ThreeRate by teachingClassCode: {} or classRoomCode: {}", teachingClassCode, classRoomCode, e);
            throw TyrfingServiceException.someThingWentWrong();
        }
    }
}
